<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-03-02 17:14:35 apple $
*/
namespace core;
defined('BY_JYA') or exit('error');
class workfans_api{
	public function upfans($wxgid,$code='',$pid=''){
		if($_SESSION['open_userid'] && $_SESSION['uid'] && $_SESSION['open_userid']!='isget'){
			return false;
		}
		$faninfo=model('core/wxwork_api',$wxgid)->get_authinfo($code);
		$files=array('id','mid','unionid','nickname','avatar','sex','tagid_list','pcd','expires_time','refresh_time','access_token','refresh_token');
		$fans=pdo_get('core_fans',array('openid'=>$faninfo['open_userid'],'type'=>9,'uniacid'=>$_SESSION['uniacid'],'wxgid'=>$wxgid,'isopen'=>1),$files);
		$_SESSION['openid']=$_SESSION['open_userid']=$faninfo['open_userid'];
		if(!$_SESSION['open_userid']){
			$_SESSION['open_userid']='isget';
			return false;
		}
		if(!$fans['id']){
			//插入粉丝数据
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'wxgid'=>$wxgid,
				'type'=>9,
				'mid'=>$_SESSION['uid']?:0,
				'openid'=>$_SESSION['openid'],
				'nickname'=>$faninfo['userid']?:'',
				'isworkman'=>$faninfo['userid']?1:0,
				'createtime'=>TIMESTAMP,
				'access_token'=>$access_token,
				'refresh_token'=>$refresh_token,
				'expires_time'=>$expires_time,
				'refresh_time'=>TIMESTAMP+27*24*3600,
				'userid'=>$faninfo['userid']?:'',
				'corpid'=>$faninfo['corpid']?:'',
				'isopen'=>1
			);
			if($pid){
				$merchid=pdo_getcolumn('core_account_wxwork_app',array('appid'=>$faninfo['corpid'],'pid'=>$pid,'merchid >'=>0,'uniacid'=>$_SESSION['uniacid']),'merchid');
			}
			if($merchid){
				$insert['merchid']=$merchid;
				$_SESSION['onemerchid']=$merchid;
			}
			pdo_insert('core_fans',$insert);
			$isregister=1;
		}
		if($fans['mid']){
				//先判断是不是关联公众号
				if($fans['mid']){
					$member=pdo_get('core_members',array('id'=>$fans['mid'],'uniacid'=>$_SESSION['uniacid']),array('id','onemid'));
					$checkid=$member['onemid']>0?$member['onemid']:$member['id'];
				}			
				if($checkid){
					model('core/member')->login($checkid);
				}else{
					$isregister=1;
				}
		}else{
			$isregister=1;
		}
		if($isregister){
				//自动注册会员
				$new_member=array(
					'uniacid'=>$_SESSION['uniacid'],
					'from_os'=>9,
					'createtime'=>TIMESTAMP,
					'nickname'=>$faninfo['userid']?:'',//留空好判断获取
					'username'=>'wxwork_'.random(10),
				);
				$uid=model('core/member')->register($new_member);
				$params=array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid'],'wxgid'=>$wxgid,'type'=>9,'isopen'=>1);
				pdo_update('core_fans',array('mid'=>$uid),$params);
		}
		return $uid?:$fans['mid'];
	}
	public function upopen_user($op='base',$code=''){
		//授权信息下根据code获取token
		$result=model('core/wxwork_api',0)->get_authinfo($code);
		$result['openid']=$result['open_userid']?:$_SESSION['openid'];
		if(!$result['openid']){
			return false;
		}
		$_SESSION['openid']=$result['openid'];
		//登录/注册会员
		$files=array('open_uid','uniacid','expires_time','refresh_time','access_token','refresh_token');
		$fans=pdo_get('core_open_user',array('open_uid'=>$_SESSION['openid']),$files);
		$uniacid=$fans['uniacid'];
		if(!$uniacid){
			$zhuce=core_setting('zhuce');
			if($zhuce['open']){
					do{
						$username='wxworker_'.random(10,true);
						$check=pdo_getcolumn('core_users',array('name'=>$username),'id');
					}while($check);
					$member=array('name'=>$username);
					$uniacid=cfc('user')->create_user($member);
					if($fans['open_uid']){
						pdo_update('core_open_user',array('uniacid'=>$uniacid),array('open_uid'=>$_SESSION['openid']));
					}else{
						$user=array(
							'uniacid'=>$uniacid,
							'open_id'=>'1006',
							'open_uid'=>$_SESSION['openid'],
							'wxgid'=>0,
						);
						pdo_insert('core_open_user',$user);
					}
					if($_SESSION['islogout']!=1){
						cfc('user')->login($uniacid);
					}
			}
		}else{
			if($_SESSION['islogout']!=1){
				cfc('user')->login($uniacid);
			}
		}
		return $uniacid;
	}
}